package wifihandler

import (
	"github.com/gin-gonic/gin"
	"github.com/shopspring/decimal"
	"time"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/wifimodel"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
)

// 获取WIFI广告单价列表
func HandleGetWifiAdSetting(ctx *gin.Context)  {
	param := &struct {
		BaseListReq request.BaseListReq
	}{}
	dingo.ErrorIf(ctx.Bind(param)).Unwrap()

	data := dao.NewWifiDao().AdSettingListDao(&param.BaseListReq)

	if data.Err != nil {
		Response(ctx)("", CodeInternalServerError, data.Data)(ServerError)
		return
	}

	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.BaseListReq.PageNo,
		PageSize:   param.BaseListReq.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  0,
		Data:       data.Unwrap(),
	})(OK)
}

// 获取服务点击日志
func HandleGetWifiServiceLog(ctx *gin.Context)  {
	param := &request.ServiceClickLogsListReq{}
	dingo.ErrorIf(ctx.ShouldBindQuery(param)).Unwrap()
	data := dao.NewWifiDao().GetWifiServiceLogList(param)
	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       data.Unwrap(),
	})(OK)
}

// 获取服务点击日志
func HandleGetWifiCashList(ctx *gin.Context)  {
	param := &request.WifiCashLogListReq{}
	dingo.ErrorIf(ctx.ShouldBindQuery(param)).Unwrap()
	data := dao.NewWifiDao().GetWifiCashLogList(param)
	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       data.Unwrap(),
	})(OK)
}

// 收益明细列表
func HandleGetWifiIncomeLog(ctx *gin.Context)  {

}

// 单价设置
func HandleSetWifiAdSetting(ctx *gin.Context)  {
	param := &wifimodel.WifiAdSetting{}
	dingo.ErrorIf(ctx.Bind(param)).Unwrap()
	param.AdMoney = decimal.NewFromInt(param.AdMoney).Mul(decimal.NewFromInt(100)).IntPart() // 单位转换为分
	param.CreateAt = dingo.Time(time.Now().Unix())
	param.UpdateAt = dingo.Time(time.Now().Unix())

	data := dao.NewWifiDao().WithdrawalSettingSaveDao(param)
	if _, b := data.Unwrap().(bool); !b {
		Response(ctx)("新增成功失败：" + data.Err.Error(), CodeUpdateSuccess, false)(OK)
		return
	}
	Response(ctx)("新增成功", CodeUpdateSuccess, true)(OK)
}
